<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;

class Mulu extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
//        $p = $request->put();
        $p = $request->post();
//        dump($p);die;
        $time = time();
        $a = array();

        $file = request()->file('detail');
//        dump($file);die;
        //移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            foreach ($file as $k => $v) {
                $info = $v->validate(['size' => 156780, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'comic');
                if ($info) {
                    $a[$k]['detail'] = $info->getSaveName();
                }else{
                    echo $file->getError();
                }
            }
        }

//        dump(empty($a));die;
        if (empty($a)){
            return $this->error('请上传作品再提交');
        }else{
            $data = [
                'zid' => $p['zid'],
                'name' => $p['name'],
                'uptime' => $time
            ];
        }

        $ds = array();
        //最新目录id
        $res = db('mulu')->insertGetId($data);
        if ($res){
            foreach ($a as $v){
                $ds = [
                    'mid'=>$res,
                    'detail'=>$v['detail']
                ];
                $rev = db('mcontent')->insert($ds);
            }
            if ($rev){
                return $this->success('编辑成功', url('admin/cartoon/index'));
            }else{
                return $this->error('编辑失败');
            }
        }else{
            return $this->error('编辑失败');
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // 是否为 Ajax 请求
        if (!Request::instance()->isAjax()) {
            return $this->error('你四不四迷路了!', '/');
        }

        // 显式查询
        $row = db('mulu')->find($id);

        $rvw = db('mcontent')->where('mid','eq',$id)->field('detail')->select();
//        var_dump($row);die;
        if ($row === null) {
            $info['status'] = false;
            $info['name'] = '查无数据';
            $info['detail'] = '查无数据,请重试';
        } else {
            $info['status'] = true;
            $info['name'] = $row['name'];
            if (!empty($rvw)){
                foreach ($rvw as $v){
                    $p[] = $v['detail'];
                }
                $info['detail'] = $p;
            }else{
                $info['detail'] = '查无数据,请重试';
            }

        }

        return json($info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $guanli = Session::get('adname');
        $row = db('mulu')->field('zid')->select();
        $p = array();
        foreach ($row as $v){
            $p[] = $v['zid'];
        }
        if (in_array($id,$p)){
            $data = db('mulu')->where('zid','eq',$id)->select();
        }else{
            $data = '暂无内容';
        }
//        dump($data);die;
        return view('mulu/mulueditd',[
            'guanli'=>$guanli,
            'data'=>$data,
            'zid' =>$id
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function editt($id)
    {
        $guanli = Session::get('adname');
        $data = db('mulu')->where('id','eq',$id)->find();
        $doe = db('mcontent')->where('mid','eq',$id)->find();
//        dump($data);die;
        return view('mulu/mulueditt',[
            'guanli'=>$guanli,
            'data'=>$data,
            'doe'=>$doe
        ]);
    }


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updatet(Request $request, $id)
    {
        //        $p = $request->put();
        $p = $request->post();
        $time = time();
        $a = array();

        $file = request()->file('detail');
        //移动到框架应用根目录/public/uploads/ 目录下
//        dump($file);

        if ($file) {
            foreach ($file as $k => $v) {
                $info = $v->validate(['size' => 156780, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'comic');
                if ($info) {
                    $a[$k]['detail'] = $info->getSaveName();
                }else{
                    echo $file->getError();
                }
            }
        }

        $data = [
            'name' => $p['name'],
            'uptime' => $time
        ];


        $res = db('mulu')->where('id','eq',$id)->update($data);
        $rev = '';
        if ($res){
            $tre = db('mcontent')->where('mid','eq',$id)->delete();
            foreach ($a as $v){
                $ds = [
                    'mid' =>$id,
                    'detail'=>$v['detail']
                ];
                $rev = db('mcontent')->insert($ds);
            }
            if ($rev){
                return $this->success('编辑成功', url('admin/cartoon/index'));
            }else{
                return $this->error('编辑失败');
            }
        }else{
            return $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = db('mulu')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        // JSON 返回
        return json($info);
    }
}
